<?php 

class Default_ProjectsController extends System_Controller
{ 
        private $projectInfo;

        public function preDispatch()
        {
             $auth = Zend_Auth::getInstance();
             if(!$auth->hasIdentity())
             {
                 $this->_redirect('/user/login');
             }
        }
            
		public function indexAction()
		{
                    
		}
		
		public function createAction()
		{
            //recebe o post de criar projeto
            $this->reciveCreateProject();
		}
                
        private function reciveCreateProject()
        {
            $form = new ZendJean_form_CreateProject();
            
            if($this->_request->isPost())
            {
                 $project = new Projects();
                
                $postData = $this->_request->getPost();
                if(!$form->isValid($postData))
                { 
                    $this->view->error = 'Algum campo est&aacute; incorreto.';   
                }
                else if($project->hasProject($postData['projectName']))
                {
                    $this->view->error = 'Projeto j&aacute; cadastrado.';
                }
                else
                {
                    $project->insertProject($postData['administerId'],$postData['projectName']);
                    $this->view->message = 'Dados devidamente cadastrados!';
                    
                }
            }
        }
        
        public function mineAction()
        {
             $auth = Zend_Auth::getInstance();
             $userData = $auth->getIdentity();
             $project = new Projects();
             $projectsResult = $project->getByUser($userData->id);
             $this->view->projectsData = $projectsResult; 
        }
        
        public function projectAction()
        {
            if($this->_request->isPost())
            {
                $postData = $this->_request->getPost();
                
                //seta a sessao do projeto
                $this->projectInfo =  new Zend_Session_Namespace('projectInfo');
                $this->projectInfo->id = $postData['projectId'];

                $this->_helper->layout()->disableLayout();
                $this->view->projectId = $postData['projectId'];
            }
            else
            {
                $this->_redirect('/projects/mine');
            }

        }
                
	}